//
//  r
//  used
//
//  Created by xalo on 16/7/14.
//  Copyright © 2016年 王凯. All rights reserved.
//

#import "KLCataListViewController.h"

@interface KLCataListViewController () <UITableViewDataSource, UITableViewDelegate>


@property (nonatomic, strong) NSMutableArray *subLists;

/** 模型 */
@property (nonatomic, strong) NSMutableArray *cateLists;

@property (nonatomic, strong) UITableView *tableView;

@property (nonatomic, strong) UITableView *subTableView;

/** indexpath */
@property (nonatomic, strong) NSIndexPath *lastIndexPath;

/** 子视图下标 */
@property (nonatomic, assign) NSInteger index;
@property (strong, nonatomic) FeHourGlass *hourGlass;

@end

@implementation KLCataListViewController

- (NSMutableArray *)subLists {
    if (!_subLists) {
        _subLists = [NSMutableArray array];
    }
    return _subLists;
}

- (NSMutableArray *)cateLists {
    if (!_cateLists) {
        _cateLists = [NSMutableArray array];
    }
    return _cateLists;
}

- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, kScreenW, kScreenH - 64) style:UITableViewStylePlain];
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.rowHeight = 100;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        
    }
    return _tableView;
}


- (UITableView *)subTableView {
    if (!_subTableView) {
        _subTableView = [[UITableView alloc] initWithFrame:CGRectMake(kScreenW, 64, kScreenW, kScreenH) style:UITableViewStylePlain];
        _subTableView.dataSource = self;
        _subTableView.delegate = self;
        _subTableView.bouncesZoom = NO;
        _subTableView.bounces = NO;
    }
    return _subTableView;
}

// 手势
- (void)swipe {
    [UIView animateWithDuration:0.25 animations:^{
        
        self.subTableView.x = kWidth;
    }];
}

- (void)viewDidLoad {
    [super viewDidLoad];

    self.automaticallyAdjustsScrollViewInsets = NO;
    self.navigationItem.title = @"全部分类";

    
    [self.view addSubview:self.tableView];
    [self.view addSubview:self.subTableView];
    
    // 添加手势
    UISwipeGestureRecognizer *swipeDown = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipe)];
    swipeDown.direction = UISwipeGestureRecognizerDirectionRight;
    [self.subTableView addGestureRecognizer:swipeDown];
    
    
    // 注册
    [self.tableView registerNib:[UINib nibWithNibName:@"KLCateListCell" bundle:nil] forCellReuseIdentifier:@"KLCataList_cell"];
    [self.subTableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"subCell"];

    
    // 请求数据
    [self loadCateData];

    self.index = 0;
    self.hourGlass = [[FeHourGlass alloc] initWithView:self.view];
    [self.view addSubview:self.hourGlass];
    [self.hourGlass show];
    
}

//请求分类数据
- (void)loadCateData {
    
    [RequestManager fetchDataWithUrl:kCateList parameter:nil method:@"GET" compeleted:^(NSData *data) {
        
        NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
        
        NSArray *cate = [KLCataList mj_objectArrayWithKeyValuesArray:dict[@"result"]];
        [self.cateLists addObjectsFromArray:cate];
        
        for (NSDictionary *dic in dict[@"result"]) {
            NSMutableArray *items = [KLSubCataList mj_objectArrayWithKeyValuesArray:dic[@"item"]];
            [self.subLists addObject:items];
        }
    } updateUI:^{
        
        [UIView animateWithDuration:0.3 animations:^{
            
            self.hourGlass.alpha = 0;
        }];
        [self.tableView reloadData];
    }];
    
}


#pragma mark - <UITableViewDataSource>
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    if (self.cateLists.count == 0) return 0;
    
    if (tableView == self.tableView) {
        return  self.subLists.count;
    }else {
        NSArray *sub = self.subLists[self.index];
        return sub.count;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    

    if (tableView == self.tableView) {
        
        KLCateListCell *cell = (KLCateListCell *)[FactoryMode productCellWithModel:self.cateLists[indexPath.row] tableView:tableView indexPath:indexPath];
        if (indexPath == self.lastIndexPath) {
            cell.arrow.hidden = NO;
        }else {
            cell.arrow.hidden = YES;
        }
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        return cell;
    }else {
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"subCell"];
        cell.textLabel.text = [self.subLists[self.index][indexPath.row] basecategoryname];
        cell.backgroundColor = KLRGBColor(245, 230, 120);
        return cell;
    }
    
}



#pragma mark - <UITableViewDelegate>
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (tableView == self.tableView) {
        
        // 滚动
        if (self.subTableView.x > kWidth * 0.5) {
            [UIView animateWithDuration:0.25 animations:^{
                self.subTableView.x = kWidth*0.5;
            }];
        }
        
        KLCateListCell *cell = [tableView cellForRowAtIndexPath:self.lastIndexPath];
        cell.arrow.hidden = YES;

        cell = [tableView cellForRowAtIndexPath:indexPath];
        cell.arrow.hidden = NO;
        
        self.index = indexPath.row;
        self.lastIndexPath = indexPath;
        [self.subTableView reloadData];
        
    }else {
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"subCell"];
        cell.textLabel.text = [self.subLists[self.index][indexPath.row] basecategoryname];
        if (self.block) {
            self.block(cell.textLabel.text);
        }
        [self.navigationController popViewControllerAnimated:YES];
    }
}

@end
